#include "tesselationshaderclass.h"


TesselationShaderClass::TesselationShaderClass():m_materialBuffer(0),m_lightBuffer(0),m_cameraBuffer(0)
{
}

TesselationShaderClass::~TesselationShaderClass()
{
	ShutdownShader();
}

bool TesselationShaderClass::Initialize(ID3D11Device* device)
{
	bool result = InitializeShader(device, L"../Engine/tesselation.vs", L"../Engine/tesselation.hs", L"../Engine/tesselation.ds", L"../Engine/tesselation.ps");
	return result;
}

bool TesselationShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, const D3DXMATRIX& worldMatrix, const D3DXMATRIX& viewMatrix, 
	const D3DXMATRIX& projectionMatrix, const D3DXVECTOR3& cameraPos, LightsContainer* i_lights, const Material* i_material)
{
	bool result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, cameraPos,i_lights, i_material);
	if(!result)
	{
		return false;
	}

	RenderShader(deviceContext, indexCount);

	return true;
}

bool TesselationShaderClass::InitializeShader(ID3D11Device* device, WCHAR* vsFilename, WCHAR* hsFilename, WCHAR* dsFilename, WCHAR* psFilename)
{
	HRESULT result;
	ID3D10Blob* errorMessage = 0;
	ID3D10Blob* vertexShaderBuffer = 0;
	ID3D10Blob* hullShaderBuffer = 0;
	ID3D10Blob* domainShaderBuffer = 0;
	ID3D10Blob* pixelShaderBuffer = 0;

	D3D11_BUFFER_DESC matrixBufferDesc;
	D3D11_BUFFER_DESC cameraBufferDesc;
	D3D11_BUFFER_DESC lightBufferDesc;
	D3D11_BUFFER_DESC materialBufferDesc;

	result = D3DX11CompileFromFile(vsFilename, NULL, NULL, "TessVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &vertexShaderBuffer, &errorMessage, NULL);
	if(FAILED(result))
	{
		OutputShaderErrorMessage(errorMessage);
		return false;
	}

	result = D3DX11CompileFromFile(hsFilename, NULL, NULL, "TessHullShader", "hs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &hullShaderBuffer, &errorMessage, NULL);
	if(FAILED(result))
	{
		OutputShaderErrorMessage(errorMessage);
		return false;
	}

	result = D3DX11CompileFromFile(dsFilename, NULL, NULL, "TessDomainShader", "ds_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &domainShaderBuffer, &errorMessage, NULL);
	if(FAILED(result))
	{
		OutputShaderErrorMessage(errorMessage);
		return false;
	}

	result = D3DX11CompileFromFile(psFilename, NULL, NULL, "TessPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &pixelShaderBuffer, &errorMessage, NULL);
	if(FAILED(result))
	{
		OutputShaderErrorMessage(errorMessage);
		return false;
	}

	result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
	if(FAILED(result))
	{
		return false;
	}

	result = device->CreateHullShader(hullShaderBuffer->GetBufferPointer(), hullShaderBuffer->GetBufferSize(), NULL, &m_hullShader);
	if(FAILED(result))
	{
		return false;
	}

	result = device->CreateDomainShader(domainShaderBuffer->GetBufferPointer(), domainShaderBuffer->GetBufferSize(), NULL, &m_domainShader);
	if(FAILED(result))
	{
		return false;
	}

	result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
	if(FAILED(result))
	{
		return false;
	}

	D3D11_INPUT_ELEMENT_DESC layout[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TANGENT",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"BINORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD",   0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
	};
	UINT numElements = sizeof( layout ) / sizeof( layout[0] );

    result = device->CreateInputLayout(layout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout );
	if(FAILED(result))
	{
		return false;
	}

	vertexShaderBuffer->Release();
	hullShaderBuffer->Release();
	domainShaderBuffer->Release();
	pixelShaderBuffer->Release();
	
	/*Constant buffers creation*/
	matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
	matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	matrixBufferDesc.MiscFlags = 0;
	matrixBufferDesc.StructureByteStride = 0;

	// Create the constant buffer pointer
	result = device->CreateBuffer(&matrixBufferDesc, 0, &m_matrixBuffer);
	if(FAILED(result))
	{
		return false;
	}

	cameraBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	cameraBufferDesc.ByteWidth = sizeof(CameraBufferType);
	cameraBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cameraBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	cameraBufferDesc.MiscFlags = 0;
	cameraBufferDesc.StructureByteStride = 0;

	result = device->CreateBuffer(&cameraBufferDesc, NULL, &m_cameraBuffer);
	if(FAILED(result))
	{
		return false;
	}

	materialBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	materialBufferDesc.ByteWidth = sizeof(MatrixBufferType);
	materialBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	materialBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	materialBufferDesc.MiscFlags = 0;
	materialBufferDesc.StructureByteStride = 0;

	result = device->CreateBuffer(&materialBufferDesc, NULL, &m_materialBuffer);
	if(FAILED(result))
	{
		return false;
	}

	lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	lightBufferDesc.ByteWidth = sizeof(LightBufferType);
	lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	lightBufferDesc.MiscFlags = 0;
	lightBufferDesc.StructureByteStride = 0;

	result = device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer);
	if(FAILED(result))
	{
		return false;
	}

	return true;
}

void TesselationShaderClass::ShutdownShader()
{
	if(m_materialBuffer)
	{
		m_materialBuffer->Release();
		m_materialBuffer = 0;
	}

	if(m_lightBuffer)
	{
		m_lightBuffer->Release();
		m_lightBuffer = 0;
	}

	if(m_cameraBuffer)
	{
		m_cameraBuffer->Release();
		m_cameraBuffer = 0;
	}

}

bool TesselationShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, const D3DXMATRIX& worldMatrix, const D3DXMATRIX& viewMatrix, const D3DXMATRIX& projectionMatrix, const D3DXVECTOR3& i_cameraPos, LightsContainer* i_lights, const Material* i_material)
{
	HRESULT result;
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	D3DXMATRIX w,v,p;

	D3DXMatrixTranspose(&w, &worldMatrix);
	D3DXMatrixTranspose(&v, &viewMatrix);
	D3DXMatrixTranspose(&p, &projectionMatrix);
	result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if(FAILED(result))
	{
		return false;
	}
	MatrixBufferType* dataPtr = (MatrixBufferType*)mappedResource.pData;
	dataPtr->world = w;
	dataPtr->view = v;
	dataPtr->projection = p;
	deviceContext->Unmap(m_matrixBuffer, 0);
	deviceContext->VSSetConstantBuffers(0, 1, &m_matrixBuffer);
	deviceContext->DSSetConstantBuffers(0, 1, &m_matrixBuffer);
	deviceContext->HSSetConstantBuffers(0, 1, &m_matrixBuffer);
	
	result = deviceContext->Map(m_cameraBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if(FAILED(result))
	{
		return false;
	}
	CameraBufferType* dataPtr3 = (CameraBufferType*)mappedResource.pData;
	dataPtr3->cameraPosition = D3DXVECTOR4(i_cameraPos, 0);	
	deviceContext->Unmap(m_cameraBuffer, 0);
	deviceContext->VSSetConstantBuffers(1, 1, &m_cameraBuffer);
	deviceContext->HSSetConstantBuffers(1, 1, &m_cameraBuffer);
	

	result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if(FAILED(result))
	{
		return false;
	}
	LightBufferType* dataLights= (LightBufferType*)mappedResource.pData;
	i_lights->CopyLightsToBuffer(dataLights);
	deviceContext->PSSetConstantBuffers(0, 1, &m_lightBuffer);
	deviceContext->Unmap(m_lightBuffer, 0);
	result = deviceContext->Map(m_materialBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if(FAILED(result))
	{
		return false;
	}
	MaterialBufferType* dataMaterial= (MaterialBufferType*)mappedResource.pData;
	dataMaterial->mKa = i_material->mKa;
	dataMaterial->mKd = i_material->mKd;
	dataMaterial->mKe = i_material->mKe;
	dataMaterial->mKs = i_material->mKs;
	dataMaterial->mShininess = i_material->mShininess;
	deviceContext->PSSetConstantBuffers(1, 1, &m_materialBuffer);
	deviceContext->Unmap(m_materialBuffer, 0);

	deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_3_CONTROL_POINT_PATCHLIST);
	
	return true;
}
